<template>
  <div>
    <custom-form
      :columns="columns"
      :rules="rules"
      v-bind="$attrs"
      v-on="listeners"
      v-model="FormData"
    >
    </custom-form>
  </div>

</template>

<script>
import customFromMixin from '@/utils/mixin/custom-form'
import curd from '@/api/curd'
export default {
  name: 'AddForm',
  mixins: [customFromMixin],
  data() {
    return {
      rules: {},
      FormData: {

      },
      // 列配置
      columns: [
        { name: '房间', field: 'room_id', opts: { control: 'room', name: 'name', required: true }, type: 'select_table' },
        { name: '上次水表读数', field: 'before_water', opts: { required: true, maxlength: 50, disabled(formData, detail) {
          return !!formData.before_water
        } }, value: 0, type: 'number', visible(formData, detail) {
          return formData.room_id
        } },
        { name: '水表读数', field: 'water', opts: { required: true, maxlength: 50 }, value: 0, type: 'number' },
        { name: '上次电表读数', field: 'before_electricity', opts: { required: true, maxlength: 50, disabled(formData, detail) {
          return !!formData.before_electricity
        } }, value: 0, type: 'number', visible(formData, detail) {
          return formData.room_id
        } },
        { name: '电表读数', field: 'electricity', opts: { required: true, maxlength: 50 }, value: 0, type: 'number' },
        { name: '日期', field: 'record_date', opts: { required: true }, type: 'date', value: new Date() }
        // { name: '性别', type: 'sex', field: 'radio', list: ['是', '否'], opts: { required: true, maxlength: 50 }, value: 1 },
        // { name: '性别', type: 'radio', field: 'sex', label: true, opts: { required: true, maxlength: 50 }, value: 1 },
        // { name: '下拉框', type: 'select', field: 'select', list: ['广东', '汕头', '潮阳'] }
        // { name: '下拉框', type: 'select', field: 'type', label: true }
        // { name: '图片', field: 'image', opts: { required: true, maxlength: 255 }, type: 'upload_image' },
        // {
        //   name: '图片',
        //   field: 'image',
        //   opts: { required: true, control: 'attachment', name: 'link', pk: 'url', query: { category: 'image' }, format: 'image' },
        //   type: 'select_table'
        // },
        // { name: '链接', field: 'link', opts: { required: true, maxlength: 255 } },
        // { name: '状态', field: 'status', label: true, type: 'radio', opts: { required: true } }
      ]
    }
  },
  watch: {
    'FormData.room_id'(newName, oldName) {
      // this.fullName = newName + ' ' + this.lastName
      console.log('newName', newName)
      // console.log('oldName', oldName)
      if (newName) {
        this.getOldData(newName)
      }
    }
  },
  methods: {
    getOldData(newName) {
      var that = this
      // console.log('获取旧数据')
      var data = {
        room_id: newName
      }
      curd('waterEleRecord').get('getWaterEle', data).then(res => {
        console.log('获取成功', res)
        var arr = res.data
        if (res.msg == '获取成功') {
          that.$set(that.FormData, 'before_water', arr.water)
          that.$set(that.FormData, 'before_electricity', arr.electricity)
        } else {
          that.$set(that.FormData, 'before_water', 0)
          that.$set(that.FormData, 'before_electricity', 0)
        }
      })

      // console.log('获取成功', res)
    }
  }
}
</script>

<style scoped>

</style>
